<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 雲溪荏苒 <290648237@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

/**
 * 视频模型
 * Class Video
 * @package app\common\model
 * @author 雲溪荏苒 <290648237@qq.com>
 * @date 2017/12/10
 */
class Video extends Model{


    /**
     * 获取视频列表
     * @param array $condition
     * @param int $curPage
     * @param int $pageSize
     * @param string $order
     * @param string $field
     * @return array
     */
    public function getList($condition = [], $curPage = 0, $pageSize = 0, $order = 'sort asc', $field = '*')
    {
        if (!$curPage) {
            $curPage = request()->param('cur_page', 1);// 获取当前页码
        }

        $count = db('video')->field($field)->where($condition)->count();
        $result = db('video')->field($field)->where($condition)->page($curPage, $pageSize)->order($order)->select();

        return [$result, $count];
    }

    /**
     * 访问量+1
     * @param $id
     * @throws \think\Exception
     */
    public static function setViewInc($id)
    {
        db('video')->where('id', $id)->setInc('view');
    }

    /**
     * 获取上一篇/下一篇
     * @param int $id
     * @return array
     */
    public function getFrontAfterVideo($id = 0)
    {

        $previous = [];
        $next = [];

        if ($id) {
            //上一篇
            $previous = db('video')->where("id<" . $id)->order('id desc')->limit('1')->find();

            //下一篇
            $next = db('video')->where("id>" . $id)->order('id asc')->limit('1')->find();

        } else {
            self::getError('视频ID错误');
        }

        return [$previous, $next];
    }

}














